<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function good()
    {
        $id = $_GET['id'];
		$uid = I('uid','','int');
        $data = D('Goods')->find($id);
		 //刷新一次浏览记录
        D("Goods")->addFootPrint($uid,$id);
        kuayu();
        echo json_encode($data);
    }
    public function goods()
   {
       $where = array();
       $where['cate_id'] = $_GET['cate_id'];
       $where['is_sale'] = 0;
       $orderby = 'id';
       $orderway = 'desc';
       if($_GET['sales']){
           $orderby = $_GET['sales'];
       }
       if($_GET['price']){
           $orderby = $_GET['shop_price'];
       }

       $data = D('Goods')->order("$orderby $orderway")
                ->where($where)
                ->select();
       kuayu();
       echo json_encode($data);
   }
    public function select()
    {
        $where = array();
        $where['cate_id'] = $_GET['cate_id'];
        $min_price = $_GET['min_price'];
        $max_price = $_GET['max_price'];
        if($_GET['brand_id']){
            $where['brand_id'] = array('in',$_GET['brand_id']);
        }
        if($_GET['region_id']){
            $where['region_id'] = array('in',$_GET['region_id']);
        }
        if($max_price == '0'){
            $where['shop_price'] = array('gt',$min_price);
        }else{
            $where['shop_price'] = array(array('gt',$min_price),array('lt',$max_price));
        }
        $data = D('Goods')->where($where)->select();

        kuayu();
        echo json_encode($data);
    }
	    //用户商品浏览记录
    public function footprint(){
        $user_id = I('user_id',10,'int');
        $p = $user_id? ['uid'=>$user_id]: array();
        $data = D('goods')->getFootPrintList($p);
//        echo "<pre>";
//        print_r($data);

        $this->ajaxReturn($data);
    }

    //用户收藏商品
    public function collection(){
        $user_id = I('user_id',10,'int');
        $p = $user_id? ['uid'=>$user_id]: array();
        $data = D('goods')->getCollectionList($p);
        $this->ajaxReturn($data);
    }
    //取消收藏
    public function cancelc(){
        $user_id = I('uid','','int');
        $cid = I('cid','','int');
        $return = D("goods")->delCollection($user_id,$cid);
        $this->ajaxReturn($return);
    }
}